function formatTime(time) {
	if (typeof time !== 'number' || time < 0) {
		return time
	}

	var hour = parseInt(time / 3600)
	time = time % 3600
	var minute = parseInt(time / 60)
	time = time % 60
	var second = time

	return ([hour, minute, second]).map(function(n) {
		n = n.toString()
		return n[1] ? n : '0' + n
	}).join(':')
}

// 时间戳日期格式化
function dateFormat(time) {
	let date = new Date(time)
	let year = date.getFullYear()
	let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
	let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
	let hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
	let minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
	let seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
	// 拼接
	return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
}

function formatLocation(longitude, latitude) {
	if (typeof longitude === 'string' && typeof latitude === 'string') {
		longitude = parseFloat(longitude)
		latitude = parseFloat(latitude)
	}

	longitude = longitude.toFixed(2)
	latitude = latitude.toFixed(2)

	return {
		longitude: longitude.toString().split('.'),
		latitude: latitude.toString().split('.')
	}
}
var dateUtils = {
	UNITS: {
		'年': 31557600000,
		'月': 2629800000,
		'天': 86400000,
		'小时': 3600000,
		'分钟': 60000,
		'秒': 1000
	},
	humanize: function(milliseconds) {
		var humanize = '';
		for (var key in this.UNITS) {
			if (milliseconds >= this.UNITS[key]) {
				humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
				break;
			}
		}
		return humanize || '刚刚';
	},
	format: function(dateStr) {
		var date = this.parse(dateStr)
		var diff = Date.now() - date.getTime();
		if (diff < this.UNITS['天']) {
			return this.humanize(diff);
		}
		var _format = function(number) {
			return (number < 10 ? ('0' + number) : number);
		};
		return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDay()) + '-' +
			_format(date.getHours()) + ':' + _format(date.getMinutes());
	},
	parse: function(str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串，转化为一个Date对象
		var a = str.split(/[^0-9]/);
		return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
	}
};


// 工具包
// 正则匹配
// 匹配帐号
const regUid = function(str) {
	return !RegExp(/^\w{6,12}$/).test(str);
}

// 匹配姓名(2至5位汉字)
const regName = function(str) {
	return !RegExp(/^[\u4e00-\u9fa5]{2,5}$/).test(str);
}

// 匹配身份证
const regIDC = function(str) {
	return !RegExp(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/).test(str);
}

// 匹配手机号
const regPhone = function(str) {
	return !RegExp(/^((1[3,5,8,7,9][0-9])|(14[1,5,6,7,9])|(16[6])|(17[0,6,7,8])|(19[7]))\d{8}$/).test(str);
}

// 匹配验证码
const regCheckNum = function(str) {
	return !RegExp(/^\d{4}$/).test(str);
}

// 匹配邮箱格式
const regEmail = function regEmail(str) {
	return !RegExp(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/).test(str);
}

const regMileage = function(str) {
	return !RegExp(/^\d+$/).test(str)
}
// 传入key获取Storage
const getStorage = function getStorage(str) {
	let data = ''
	uni.getStorage({
		key: str,
		success: function(res) {
			data = res.data
		}
	})
	return data
}

// 传入key移除Storage
const removeStorage = function removeStorage(str) {
	// uni.removeStorage(str);
	uni.removeStorageSync(str);

}

/* 
 * uView input类型表单校验, 默认触发方式为失去焦点
 * 参数说明: 
 * str: 默认显示的提示信息
 * func: 需调用的正则表达式方法
 * msg: 输入有误的提示信息 
 */
const ruleInput = function(str, func, msg) {
	const rule = [{
		required: true,
		message: str,
		// 可以单个或者同时写两个触发验证方式 
		trigger: ['blur']
	}, {
		validator: (rule, value, callback) => {
			return !eval(func)(value)
		},
		message: msg,
		trigger: ['blur']
	}]
	return rule
}


const ruleNumber = function(str,func,msg){
	const rule = [{
		required: true,
		message: str,
		trigger: ['blur']
	},{
		validator: (rule, value, callback) => {
			return !eval(func)(value)
		},
		message: msg,
		trigger: ['blur']
	}]
}
/* 
 * uView input类型表单校验(仅必填), 默认触发方式为失去焦点
 * 参数说明: 
 * msg: 输入有误的提示信息 
 */
const ruleInputRequired = function(msg) {
	const rule = [{
		required: true,
		message: msg,
		trigger: ['blur']
	}]
	return rule
}

/* 
 * uView select类型表单校验, 默认触发方式为值改变时
 * 参数说明: 
 * msg: 输入有误的提示信息 
 */
const ruleSelect = function(msg) {
	const rule = [{
		required: true,
		message: msg,
		trigger: ['change'],
	}]
	return rule
}

const dateSelect = function(msg) {
	const rule = [{
		required: true,
		message: msg,
		trigger: ['change'],
	}]
	return rule
}

module.exports = {
	formatTime: formatTime,
	dateFormat: dateFormat,
	formatLocation: formatLocation,
	dateUtils: dateUtils,
	regUid: regUid,
	regPhone: regPhone,
	regName: regName,
	regIDC: regIDC,
	regEmail: regEmail,
	regCheckNum: regCheckNum,
	getStorage: getStorage,
	removeStorage: removeStorage,
	ruleInput: ruleInput,
	ruleInputRequired: ruleInputRequired,
	ruleSelect: ruleSelect,
	dateSelect: dateSelect,
	ruleNumber: ruleNumber
}
